package com.huluobo.a2308douyindemo.shop

import android.util.Log
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.a2308douyindemo.R
import com.huluobo.a2308douyindemo.adapter.CartAdapter
import com.huluobo.a2308douyindemo.bean.CartBean
import com.huluobo.a2308douyindemo.databinding.ActivityCartBinding
import com.huluobo.a2308douyindemo.mvvm.BaseActivity
import com.huluobo.a2308douyindemo.net.Const
import com.huluobo.a2308douyindemo.utils.MyToast
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class CartActivity : BaseActivity<ActivityCartBinding, ShopViewModel>() {
    private lateinit var cartAdapter: CartAdapter
    private lateinit var cartBean: CartBean

    override fun initData() {
        vm.getCart()
        vm.getCartSuccess.observe(this) {
            cartAdapter.data.clear()
            cartAdapter.data.addAll(it.data)
            cartAdapter.notifyDataSetChanged()
        }

        vm.addToCartSuccess.observe(this) {
            Log.e("TagA", "修改购物车数量成功")
        }

        vm.deleteCartSuccess.observe(this) {
            MyToast.showLong("删除成功")
            //adapter的remove方法可以直接移除一个Bean类,并且展示删除动画
            cartAdapter.remove(cartBean)
        }
    }

    override fun initView() {
        binding.cartRv.layoutManager = LinearLayoutManager(this)
        cartAdapter = CartAdapter()
        binding.cartRv.adapter = cartAdapter

        //初始化进入页面时,计算一次总价
        totalPrice()

        cartAdapter.addChildClickViewIds(
            R.id.item_cart_cb,
            R.id.item_cart_remove_btn,
            R.id.item_cart_add_btn,
            R.id.item_cart_delete_iv
        )
        cartAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.item_cart_cb -> {
                    //是否选中取反值
                    cartAdapter.data[position].isChecked = !cartAdapter.data[position].isChecked
                    //单独刷新item,不刷新整个适配器
                    (adapter.getViewByPosition(position, R.id.item_cart_cb) as CheckBox).isChecked =
                        cartAdapter.data[position].isChecked
                }

                R.id.item_cart_remove_btn -> {
                    if (cartAdapter.data[position].count > 1) {
                        cartAdapter.data[position].count--
                        (adapter.getViewByPosition(position, R.id.item_cart_num_tv) as TextView).text =
                            "${cartAdapter.data[position].count}"

                        val map = mapOf(
                            Const.PARAM_GOODS_ID to cartAdapter.data[position].goods_id,
                            Const.PARAM_COUNT to -1
                        )
                        vm.addToCart(map)

                    } else {
                        MyToast.showLong("购物车数量最小为1")
                    }
                }

                R.id.item_cart_add_btn -> {
                    cartAdapter.data[position].count++
                    (adapter.getViewByPosition(position, R.id.item_cart_num_tv) as TextView).text =
                        "${cartAdapter.data[position].count}"

                    val map = mapOf(
                        Const.PARAM_GOODS_ID to cartAdapter.data[position].goods_id,
                        Const.PARAM_COUNT to 1
                    )
                    vm.addToCart(map)
                }

                R.id.item_cart_delete_iv -> {
                    cartBean = cartAdapter.data[position]
                    val map = mapOf(
                        Const.PARAM_IDS to cartBean.id
                    )
                    vm.deleteCart(map)
                }
            }

            checkAll()
            //只要点击了item中的内容,就需要重新计算一次总价,比如删除,加减购物车,是否选中
            totalPrice()
        }

        binding.cartSelectAllCb.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.cartSelectAllCb.setOnClickListener {
                cartAdapter.data.forEach {
                    it.isChecked = isChecked
                }
                cartAdapter.notifyDataSetChanged()
                totalPrice()
            }
        }
    }

    private fun totalPrice() {
        //总价
        var totalPrice = 0

        cartAdapter.data.forEach {
            if (it.isChecked) {
                totalPrice += it.goods_default_price * it.count
            }
        }

        binding.cartTotalPriceTv.text = "总价 : ￥$totalPrice"
    }

    //检查全选的方法
    private fun checkAll() {
        //用于存放所有是否选中的集合
        val checkList = mutableListOf<Boolean>()
        cartAdapter.data.forEach {
            //将购物车列表中所有的选中的值保存到集合中
            checkList.add(it.isChecked)
        }

        //检查false是否在当前集合中,如果在,全选就是false,如果不在,全选就是true
        binding.cartSelectAllCb.isChecked = false !in checkList
    }
}